import requests
from urllib.parse import quote
import time

base_url = "https://lf-cdn.trae.com.cn/obj/trae-com-cn/pkg/app/releases/stable/{version}/win32/Trae%20CN-Setup-x64.exe"

def download_file(url, version):
    try:
        response = requests.get(url, stream=True, timeout=10)
        if response.status_code == 200:
            filename = f"Trae_CN-Setup-x64_{version}.exe"
            with open(filename, 'wb') as f:
                for chunk in response.iter_content(1024):
                    f.write(chunk)
            print(f"成功下载版本: {version}")
            return True
        return False
    except Exception as e:
        print(f"版本 {version} 下载失败: {str(e)}")
        return False

def main():
    # 从 1.0.12385 开始递减
    major, minor, patch = 1, 0, 12384
    
    while patch > 0:  # 设置最小版本号限制
        version = f"{major}.{minor}.{patch}"
        encoded_version = quote(version, safe='')
        url = base_url.format(version=encoded_version)
        
        print(f"正在尝试版本: {version}")
        if download_file(url, version):
            break  # 下载成功则停止
            
        patch -= 1  # 版本号递减
        time.sleep(1)  # 避免请求过于频繁

if __name__ == "__main__":
    main()